<template>
  <div class="login-container">
    <div :span="18" class="left-side" />
    <div :span="6" class="right-side" :loading="loading">
      <div class="login-form">
        <div class="title-container">
          <h3 class="title">自动登录中....</h3>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getUserpwd, getUkeyUrl } from '@/api/user'
import NProgress from 'nprogress'

NProgress.configure({ showSpinner: false })

export default {
  name: 'AutoLogin',
  components: { },
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loading: false,
      redirect: undefined,
      otherQuery: {},
      firstlogin: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = 'Dashboard'
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted() {
    // UKEY登录
    this.afterUKeyLogin()
  },
  destroyed() {
  },
  methods: {
    getUserpwd,
    getUkeyUrl,
    // 手动登录
    handleLogin() {
      this.loading = true
      this.$store.dispatch('user/login', this.loginForm)
        .then(() => {
          location.reload()
        })
        .catch(e => {
          console.error(e)
        }).finally(() => {
          this.loading = false
          NProgress.done()
        })
    },
    getOtherQuery(query) {
      var obj = Object.keys(query)

      return obj.reduce && obj.reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },
    // 判断是否Ukey登录
    async afterUKeyLogin() {
      const item = await this.getUkeyUrl()

      const ukeyurl = item.URL
      if (ukeyurl === undefined || ukeyurl === '') {
        this.$message.error('未配置UKEY访问地址！')
        return
      }

      NProgress.start()
      // 解析URL
      const locurl = window.location.href
      const index = locurl.indexOf('username=')

      if (index > 0) {
        const secindex = locurl.indexOf('##')
        const username = locurl.substring(index, secindex).replace('username=', '')

        console.log(username)

        const userInfo = await this.getUserpwd(decodeURIComponent(username))
        this.loginForm.username = userInfo.userid
        this.loginForm.password = userInfo.password

        this.handleLogin()
        return
      }

      NProgress.done()

      // 反复登录
      // window.location.href = ukeyurl
    }
  }
}
</script>

<style lang="scss" scoped>
$bg:#fff;
$dark_gray:#333;
$light_gray:#B2B2B2;

.login-container {
  position: relative;
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  .left-side{
    display: inline-block;
    width: calc(100% - 440px);
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    background: url('/static/img/login-background.jpg') no-repeat;
    background-size: 100%;
    background-position: center;
    background-color:#8b9797;
  }
  .right-side{
    position: absolute;
    right: 0;
    width: 440px;
    padding: 20px;
    .login-form {
      width: 100%;
      padding: 300px 35px 0;
      // margin: 0 auto;
      overflow: hidden;
      /deep/.el-input {
        display: inline-block;
        height: 47px;
        width: 85%;

        input {
          background: transparent;
          border: 0px;
          -webkit-appearance: none;
          border-radius: 0px;
          padding: 12px 5px 12px 15px;
          height: 47px;
        }
      }

      .el-form-item {
        margin-bottom: 40px;
        &, *, /deep/.el-input *{
          transition: .3s;
        }
        border-bottom: 1px solid #b8b8b8;
        // background: rgba(0, 0, 0, 0.1);
        color: #454545;
        &:focus-within{
          .svg-container, .show-pwd {
            color: #18a689;
          }
          /deep/.el-input{
            input {
              color: #18a689;
            }
          }
          border-bottom: 1px solid #18a689;
          color: #18a689;
        }
      }
      .login-button{
        width:100%;
        margin-top:10px;
        background: #18a689;
      }
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;
      margin-bottom: 80px;
      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
}
</style>
